<template>

  <div class="count-down" v-if="show">
    <p v-if="status==2">距离收货时间: {{day}}天{{hour}}时{{minute}}分{{seccond}}秒</p>
    <p v-if="status==0">订单将在{{hour}}时{{minute}}分{{seccond}}秒后取消</p>
  </div>

</template>

<script>
  export default {
    name: "countDown",
    props: {
      createtime: {
        type: String,
        default: 0
      },
      takseTime:{
        type:String,
        default:0
      },
      status: {
        type: Number,
        default: 0
      }

    },
    methods: {
      //自动收货倒计时
      Deliverycountdown() {
        var createTime = Date.parse(this.takseTime)
        var takegoodTime = createTime + 120000
        this.countDownTime = (takegoodTime - new Date().getTime()) / 1000
        var that = this
      var time=  setInterval(function (res) {
          that.countDownTime -= 1
          that.day = Math.floor(that.countDownTime / 86400);
          that.hour = Math.floor((that.countDownTime % 86400) / 3600);
           if (that.hour<0){
             clearInterval(time)
             that.show=false
           }
          that.minute = Math.floor(((that.countDownTime % 86400) % 3600) / 60);
          that.seccond = Math.floor(((that.countDownTime % 86400) % 3600) % 60);
        }, 1000)

      },
      //订单取消倒计时
      Ordercancellationcountdown() {
        var that=this
        var createTime = Date.parse(this.createtime)
        var takegoodTime = createTime + 120000
        this.countDownTime = (takegoodTime - new Date().getTime()) / 1000
        var timer = setInterval(function (res) {
          that.countDownTime -= 1
          that.hour = Math.floor((that.countDownTime % 86400) / 3600);
          that.minute = Math.floor(((that.countDownTime % 86400) % 3600) / 60);
          that.seccond = Math.floor(((that.countDownTime % 86400) % 3600) % 60);
          if (that.hour < 0) {
            clearInterval(timer)
            that.show = false
          }
        }, 1000)
      }
    },
    data() {

      return {
        countDownTime: 0,
        day: 0,
        hour: 0,
        minute: 0,
        seccond: 0,
        show: true,
      }
    },
    created() {

      this.$nextTick(()=>{
        switch (this.status) {

          case 2: {
            this.Deliverycountdown();
            break
          }
          case 0: {
            this.Ordercancellationcountdown();
            break
          }
        }


      })
    },
    mounted() {

    },
    // watch: {
    //   status: {
    //     handler(n, o) {
    //       // do something
    //       // switch (o) {
    //       //   case 1:{this.Deliverycountdown();break}
    //       //   case 0:{this.Ordercancellationcountdown();break}
    //       // }
    //     },
    //     deep: true// 深度监听父组件传过来对象变化
    //   }
    //
    // }
  }
</script>

<style scoped>

  .count-down {
    position: relative;
    height: 40px;
    width: 25%;

  }

  .count-down p {
    position: absolute;
    line-height: 50px;
    color: #ff6700;
    font-size: 16px;
    right: 10px;
  }
</style>
